package cn.hiapi.admin.api;

import cn.hiapi.admin.query.ActivityQuery;
import cn.hiapi.admin.vo.ActivityVo;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.activity.ActivityCategory;
import cn.hiapi.shopkeeper.entity.activity.ActivityOther;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.service.activity.*;
import cn.hiapi.utils.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2022/12/30 15:14
 */
@RestController("manageActivityController")
@RequestMapping("/manage/activity")
public class ActivityController extends BasicQueryController<Activity, Long, ActivityVo, ActivityQuery> {
    protected ActivityController(ActivityService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }


    @Override
    public ActivityQuery setQuery(ActivityQuery query) {
        if (StringUtils.isNotEmpty(query.getShopName())) {
            List<Shop> shops = this.dispatchContext.getServiceOne(ShopService.class).findList(new QueryWrapper().like("name", "%" + query.getShopName() + "%"));
            if (shops.isEmpty()) throw new BasicException( "店铺名不存在");
            query.setShopId(shops.stream().map(Shop::getId).collect(Collectors.toList()));
        }

        return super.setQuery(query);
    }

    @Override
    public List<ActivityVo> toListVo(List<Activity> list, String[] fields) {
        return super.toList(list, fields);
    }

    @Override
    public ActivityVo toData(Activity data, String[] fields) {
        ActivityVo vo = data.copy(new ActivityVo());
        JSONObject expand  = this.dispatchContext.getServiceOne(ActivityFactory.class).get(data.getType()).getExpand(data.getId());
        vo.setExpand(expand);
        ActivityOther other = this.dispatchContext.getServiceOne(ActivityOtherService.class).getByAid(data.getId());
        vo.setOther(other);

        ActivityCategory category = this.dispatchContext.getServiceOne(ActivityCategoryService.class).get(data.getCid());
        if (category != null)
            vo.setCategory(category.getName());
        Shop shop = dispatchContext.getServiceOne(ShopService.class).get(data.getShopId());
        if (shop != null) {
            vo.setShop(JsonUtils.builder()
                    .put("name", shop.getName())
                    .put("logo", shop.getLogo())
                    .build());
        }
        return vo;
    }
}
